/**
 * 用户数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.UserGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,

	initComponent: function(){
		
		var gridStructure = [{
			name: 'userId',
			hidden: true
		},{
			name: 'loginName',  
			header: "登录名称",
			sortable: true,
			width: 20
		},{
			name: 'userCode',  
			header: "用户编号",
			sortable: true,
			width: 20
		},{
			name: 'userName',  
			header: "用户姓名",
			sortable: true,
			width: 20
		},{
			name: 'sexLabel',  
			header: "性别",
			sortable: true,
			width: 10
		},{
			name: 'positionLabel',  
			header: "在岗情况",
			sortable: true,
			width: 15
		},{
			name: 'officeTel',  
			header: "办公电话",
			sortable: true,
			width: 20
		},{
			name: 'mobile',  
			header: "手机号码",
			sortable: true,
			width: 20
		},{ 
			type:'boolean',
			name: 'isActive',  
			header: "是否启用",
			sortable: true,
			width: 15
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'loginName';
		this.keyField = 'userId';
		
		this.deleteMsg = '您确认要删除选中的用户吗？';
		
		Ext.ux.grid.UserGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(userGrid) {
				var record = userGrid.getSelectionModel().getSelected();
				if(record != null){
					var userId = record.get('userId');
					this.showUserInfo(userId);
				}
			});
		}
		
		this.on('updateRecord', function(userId){
			this.showUserInfo(userId);
		});
		
//		this.on('deleteRecord', function(grid, userIds){
//			//this.deleteUser(userIds);
//		});
	},
	
	showUserInfo: function(userId){
		var win = new Ext.ux.org.omorg.UserDetailWindow({id:'userDetailWindow', title:'修改用户信息', userId:userId, orgId:'-1'});
		win.showUserInfo();
	},
	//给机构或者机构岗位添加用户
	addUser: function(orgId ,posId){
		var win = new Ext.ux.org.omorg.UserDetailWindow({id:'userDetailWindow', title:'新增用户', orgId:orgId, posId:posId});
		win.show();
	},
	//给工作组添加用户
	addUserToGroup: function(orgId, groupId){
		var win = new Ext.ux.org.omgroup.SelectUserWindow({
				title: '新增工作组用户',
				url: 'om-user!getUsersInOrgButNotInGroup.action?orgId='+ orgId + '&groupId=' + groupId,
				groupId: groupId
			});
		win.show();
	},
	//给工作组岗位添加用户
	addUserToGroupPos: function(orgId, groupId, posId){
		var win = new Ext.ux.org.omgroup.SelectUserWindow({
			title: '新增工作组岗位用户', 
			url: 'om-user!getUsersNotHavePos.action?orgId='+ orgId + '&posId=' + posId,
			orgId: orgId,
			groupId: groupId,
			posId: posId
		});
		win.show();
	},
	deleteUser: function(userIds) {
		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的用户吗？<br/><font color="red">注意</font>：删除用户将同时删除与用户相关的组织和权限关联信息。',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
		    	if(btn == 'yes'){
					Ext.Ajax.request({
						url:'om-user!delete.action',
						success:function(){
							this.getStore().reload();
							var orgTree = Ext.getCmp('orgTree'); 
							if(orgTree != null)
								orgTree.refreshOrg();
						},
						method: 'POST',
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除用户失败！');
						},
						params:{
							userIds: userIds
						},
						waitMsg: '正在删除数据，稍候...',
						scope: this
					});
				}
			},
			width: 350,
		    scope: this
		});
	}
});